﻿#!/usr/bin/python3

import os
import ar8030
import ar8030_callback
import json
import sys
from time import sleep


if __name__ == "__main__":
    # connect host
    try :
        host = ar8030.bb_host_t('192.168.30.50', 50000)
    except OSError as p:
        print("can't connect to remote")
        exit()

    # get uart list
    getlist = ar8030.bb_uart_get_list(host)
    name0 = getlist[1]

    # setup uart
    puart = ar8030.uart_ioctl()
    puart.uart_dev_name = name0
    ret =  ar8030.bb_uart_setup(host,puart)
    print("get res = " , ret)

    # get list
    try :
        devlist = ar8030.bb_dev_list_t(host)
        num = devlist.get_num()
        print("get list num = " ,num)
    except OSError as p:
        exit()
    # get one device
    devone = ar8030.bb_dev_getlist_index(devlist, 0)
    # open device
    hdl = ar8030.bb_dev_open(devone)

    inar = ar8030.bb_set_plot_t()
    inar.user = 8
    inar.enable = 1
    inar.cache_num = ar8030.BB_PLOT_POINT_MAX

    retio = ar8030.bb_ioctl(hdl, ar8030.BB_SET_PLOT, inar, None)

    # callback test
    class linkcallback(ar8030_callback.evt_callback):
        def run(self, dat):
            plot = ar8030_callback.void_to_plot_data(dat)
            plotdata = plot[0]
            print('This print from Python: n = ',
                  plot.user, 'snr = ', plotdata.snr)

    linkcb = linkcallback()
    ar8030_callback.set_cb(hdl, ar8030.BB_EVENT_PLOT_DATA, linkcb)

    while True:
        sleep(1000)

    # get status
    inar = ar8030.bb_get_status_in_t()
    inar.user_bmp = 0x3fff
    outar = ar8030.bb_get_status_out_t()
    retio = ar8030.bb_ioctl(hdl, ar8030.BB_GET_STATUS, inar, outar)
    print("mac[0] = ", outar.mac[0])
    # socket opt
    opt = ar8030.bb_sock_opt_t()
    opt.rx_buf_size = 40000
    opt.tx_buf_size = 40000
    fd = ar8030.bb_socket_open(hdl, ar8030.BB_SLOT_0, 1, 3, opt)
    ar8030.bb_socket_close(fd)
    print("fin")
